package com.klasster.recruitmentPlatform.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.klasster.recruitmentPlatform.common.response.Result;
import com.klasster.recruitmentPlatform.common.response.ResultCode;
import com.klasster.recruitmentPlatform.domain.dto.CompanyDTO;
import com.klasster.recruitmentPlatform.domain.dto.CompanyJoinApplicationDTO;
import com.klasster.recruitmentPlatform.domain.po.Company;
import com.klasster.recruitmentPlatform.domain.po.CompanyJoinApplication;
import com.klasster.recruitmentPlatform.domain.po.Recruiter;
import com.klasster.recruitmentPlatform.domain.vo.CompanyJoinApplicationVO;
import com.klasster.recruitmentPlatform.domain.vo.CompanySelectionVO;
import com.klasster.recruitmentPlatform.service.impl.CompanyJoinApplicationServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.CompanyServiceImpl;
import com.klasster.recruitmentPlatform.service.impl.RecruiterServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/company")
public class CompanyController {

    private final CompanyServiceImpl companyService;
    private final CompanyJoinApplicationServiceImpl companyJoinApplicationService;
    private final RecruiterServiceImpl recruiterService;

    @Transactional
    @PostMapping("/registerCompany")
    public Result registerCompany(@RequestBody CompanyDTO companyDTO) {
        //判断公司名称是否已经存在
        if(companyService.getByCompanyName(companyDTO.getCompany_name())!=null) {
            return Result.error(ResultCode.COMPANY_EXIST,false);
        }
        Company company = BeanUtil.copyProperties(companyDTO,Company.class);
        company.setState(0);
        return Result.success(ResultCode.COMPANY_SAVE_SUCCESS,companyService.save(company));
    }

    @GetMapping("/isOperable/{company_id}")
    public Result isOperable(@PathVariable Long company_id) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",company_id).eq("state",1);
        return Result.success(ResultCode.COMPANY_GET_SUCCESS,companyService.getOne(queryWrapper)!=null);
    }


    @GetMapping("/getCompanyInfo/{company_id}")
    public Result getCompanyInfo(@PathVariable Long company_id) {
        return Result.success(ResultCode.COMPANY_GET_SUCCESS,companyService.getById(company_id));
    }

    /**
     * 获取企业所有者的企业信息
     */
    @GetMapping("/getOwnerCompanyInfo/{owner_id}")
    public Result getOwnerCompanyInfo(@PathVariable Long owner_id) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_id", owner_id);
        Company company = companyService.getOne(queryWrapper);
        if(company==null) {
            return Result.success(ResultCode.COMPANY_GET_SUCCESS,null);
        }
        return Result.success(ResultCode.COMPANY_GET_SUCCESS,company);
    }

    /**
     * 是否为企业所有者（提交过注册信息）
     */
    @GetMapping("/hasOwnerCompany/{owner_id}")
    public Result hasOwnerCompany(@PathVariable Long owner_id) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("owner_id", owner_id);
        Company company = companyService.getOne(queryWrapper);
        return Result.success(ResultCode.COMPANY_GET_SUCCESS,company!=null);
    }


    @Transactional
    @DeleteMapping("/deleteCompanyInfo/{company_id}")
    public Result deleteCompanyInfo(@PathVariable Long company_id) {
        return Result.success(ResultCode.COMPANY_DELETE_SUCCESS,companyService.removeById(company_id));
    }

    @Transactional
    @PutMapping("/updateCompanyInfo")
    public Result updateCompanyInfo(@RequestBody Company company) {
        return Result.success(ResultCode.COMPANY_SAVE_SUCCESS,companyService.updateById(company));
    }

    /**
     * 获取公司选项列表
     */
    @GetMapping("/getCompanySelection")
    public Result getCompanySelection() {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",1);
        List<Company> companies = companyService.list(queryWrapper);
        List<CompanySelectionVO> companySelectionVOS = BeanUtil.copyToList(companies, CompanySelectionVO.class);
        return Result.success(ResultCode.COMPANY_GET_SUCCESS, companySelectionVOS);
    }

    /**
     * 获取公司所有者需审核的加入申请列表
     */
    @GetMapping("/getCompanyOwnerJoinApplications/{owner_id}")
    public Result getCompanyOwnerJoinApplications(@PathVariable Long owner_id) {
        Company c = companyService.getByOwnerID(owner_id);
        //如果没有拥有公司，则无需获取申请加入列表
        if(c==null) {
            return Result.success(ResultCode.COMPANY_GET_JOIN_APPLICATION_SUCCESS,false);
        }
        //否则获取该公司所有者的申请加入列表
        QueryWrapper<CompanyJoinApplication> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",c.getCompany_id());
        List<CompanyJoinApplication> companyJoinApplications = companyJoinApplicationService.list(queryWrapper);
        List<CompanyJoinApplicationVO> companyJoinApplicationVOS = BeanUtil.copyToList(companyJoinApplications,CompanyJoinApplicationVO.class);
        companyJoinApplicationVOS.forEach(e -> {
            Recruiter applicant = recruiterService.getById(e.getApplicant_id());
            Company company = companyService.getById(e.getCompany_id());
            e.setApplicant_name(applicant.getName());
            e.setCompany_icon(company.getCompany_icon());
            e.setCompany_name(company.getCompany_name());
        });
        return Result.success(ResultCode.COMPANY_GET_JOIN_APPLICATION_SUCCESS,companyJoinApplicationVOS);
    }

    /**
     * 获取招聘者提交的公司加入申请列表
     */
    @GetMapping("/getCompanyApplicantJoinApplications/{applicant_id}")
    public Result getCompanyApplicantJoinApplications(@PathVariable Long applicant_id) {
        QueryWrapper<CompanyJoinApplication> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("applicant_id",applicant_id);
        List<CompanyJoinApplication> companyJoinApplications = companyJoinApplicationService.list(queryWrapper);
        List<CompanyJoinApplicationVO> companyJoinApplicationVOS = BeanUtil.copyToList(companyJoinApplications,CompanyJoinApplicationVO.class);
        Recruiter applicant = recruiterService.getById(applicant_id);
        companyJoinApplicationVOS.forEach(e -> {
            Company company = companyService.getById(e.getCompany_id());
            e.setApplicant_name(applicant.getName());
            e.setCompany_icon(company.getCompany_icon());
            e.setCompany_name(company.getCompany_name());
        });
        return Result.success(ResultCode.COMPANY_GET_JOIN_APPLICATION_SUCCESS,companyJoinApplicationVOS);
    }

    /**
     * 申请加入公司
     */
    @Transactional
    @PostMapping("/applyJoinCompany")
    public Result applyJoinCompany(@RequestBody CompanyJoinApplicationDTO companyJoinApplicationDTO) {
        CompanyJoinApplication companyJoinApplication = BeanUtil.copyProperties(companyJoinApplicationDTO,CompanyJoinApplication.class);
        // 待审核状态
        companyJoinApplication.setState(0);
        return Result.success(ResultCode.COMPANY_APPLY_JOIN_APPLICATION_SUCCESS,companyJoinApplicationService.save(companyJoinApplication));
    }

    /**
     * 同意申请加入公司
     */
    @Transactional
    @PutMapping("/agreeJoinCompany/{id}")
    public Result agreeJoinCompany(@PathVariable Long id) {
        CompanyJoinApplication companyJoinApplication = companyJoinApplicationService.getById(id);
        // 通过状态
        companyJoinApplication.setState(1);
        // 设置对应申请者的公司id
        Recruiter recruiter = recruiterService.getById(companyJoinApplication.getApplicant_id());
        recruiter.setCompany_id(companyJoinApplication.getCompany_id());
        recruiterService.updateById(recruiter);
        return Result.success(ResultCode.COMPANY_UPDATE_JOIN_APPLICATION_SUCCESS,companyJoinApplicationService.updateById(companyJoinApplication));
    }

    /**
     * 拒绝申请加入公司
     */
    @Transactional
    @PutMapping("/disagreeJoinCompany/{id}")
    public Result disagreeJoinCompany(@PathVariable Long id) {
        CompanyJoinApplication companyJoinApplication = companyJoinApplicationService.getById(id);
        // 未通过状态
        companyJoinApplication.setState(2);
        // 发出通知
        return Result.success(ResultCode.COMPANY_UPDATE_JOIN_APPLICATION_SUCCESS,companyJoinApplicationService.updateById(companyJoinApplication));
    }

    /**
     * 取消加入公司申请
     */
    @Transactional
    @PutMapping("/cancelJoinCompany/{id}")
    public Result cancelJoinCompany(@PathVariable Long id) {
        CompanyJoinApplication companyJoinApplication = companyJoinApplicationService.getById(id);
        // 撤销状态
        companyJoinApplication.setState(3);
        return Result.success(ResultCode.COMPANY_CANCEL_JOIN_APPLICATION_SUCCESS,companyJoinApplicationService.updateById(companyJoinApplication));
    }

    /**
     * 获取公司人员列表
     */
    @GetMapping("/getCompanyStaff/{company_id}")
    public Result getCompanyStaff(@PathVariable Long company_id) {
        QueryWrapper<Recruiter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",company_id);
        List<Recruiter> recruiters = recruiterService.list(queryWrapper);
        return Result.success(ResultCode.COMPANY_GET_STUFF_SUCCESS,recruiters);
    }

    /**
     * 移除企业招聘人员
     */
    @Transactional
    @PutMapping("/removeCompanyStaff/{recruiter_id}")
    public Result removeCompanyStaff(@PathVariable Long recruiter_id) {
        UpdateWrapper<Recruiter> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("recruiter_id",recruiter_id);
        updateWrapper.set("company_id", null);
        updateWrapper.set("title", null);
        return Result.success(ResultCode.COMPANY_MODIFY_STUFF_SUCCESS,recruiterService.update(updateWrapper));
    }

    /**
     * 修改招聘人员头衔
     */
    @Transactional
    @PutMapping("/updateRecruiterTitle")
    public Result updateRecruiterTitle(@RequestParam Long recruiter_id, @RequestParam String title) {
        if(title.equals("null"))
            title = null;
        UpdateWrapper<Recruiter> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("recruiter_id",recruiter_id);
        updateWrapper.set("title",title);
        return Result.success(ResultCode.COMPANY_MODIFY_STUFF_SUCCESS,recruiterService.update(updateWrapper));
    }

}
